﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.ObjectModel;
using Negocio;

namespace Persistencia
{
    class BrokerTestValue: Broker
    {
        public BrokerTestValue()
        {
            this.conn = ConnectionDB.getInstance();
        }

        public override void add(Object o)
        {
            TestValue testValue = (TestValue)o;
            this.conn.connect();
            string sql = "insert into TestValue (value_,oidTest,oid) values('" + testValue.ValueExecution + "'," + testValue.Test.OID + "," + testValue.OID + ")";
            this.conn.execute(sql);
            string sql1 = "update OID set OId=" + testValue.OID;
            this.conn.execute(sql1);
        }

        public override void update(Object o)
        {
            TestValue testValue = (TestValue)o;
            this.conn.connect();
            string sql = "update TestValue set oidTest='" + testValue.Test.OID + "',value_= '" + testValue.ValueExecution + "' where oid = " + testValue.OID;
            this.conn.execute(sql);
        }

        public override void delete(Object o)
        {
            /* Los test no se borran*/
        }

        public override void bringData(Object o)
        {
            TestValue testValue = (TestValue)o;
            this.conn.connect();
            string sql1 = "Select * from TestValue where oid=" + testValue.OID;
            this.conn.bringList(sql1);
            while (this.conn.nextRecord())
            {
                TestValue test = new TestValue();
                test.Test.OID = int.Parse(this.conn.getDataString("oidTest").ToString());
                test.ValueExecution = int.Parse(this.conn.getDataString("value_").ToString());
                test.OID = int.Parse(this.conn.getDataString("oid").ToString());                
            }
            this.conn.disconnect();
        }

        public override ObservableCollection<Object> bringList(Object o)
        {
            ObservableCollection<Object> list = new ObservableCollection<Object>();
            string sql1 = "Select * from TestValue";
            this.conn.bringList(sql1);
            while (this.conn.nextRecord())
            {
                TestValue test = new TestValue();
                test.Test.OID = int.Parse(this.conn.getDataString("oidTest").ToString());
                test.ValueExecution = int.Parse(this.conn.getDataString("value_").ToString());
                test.OID = int.Parse(this.conn.getDataString("oid").ToString());
                list.Add(test);
            }
            this.conn.disconnect();
            return list;
        }
    }

}